package com.danco.training.zaneuskaya.service.impl;


import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.danco.training.zaneuskaya.domain.Message;
import com.danco.training.zaneuskaya.domain.User;
import com.danco.training.zaneuskaya.repository.MessageDao;
import com.danco.training.zaneuskaya.repository.UserDao;
import com.danco.training.zaneuskaya.service.MessageService;
/**
 * @author Zaneuskaya Aliaksandra
 *
 */
@Service("messageService")
@Transactional
public class MessageServiceImpl implements MessageService {

	private static final String MESSAGE_NOT_FOUND = "Message not found";

	private static final Logger logger = Logger
			.getLogger(MessageServiceImpl.class);

	@Autowired
	private MessageDao messageDao;

	@Autowired
	private UserDao userDao;

	public UserDao getUserDao() {
		return userDao;
	}

	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}

	public MessageDao getMessageDao() {
		return messageDao;
	}

	public void setMessageDao(MessageDao messageDao) {
		this.messageDao = messageDao;
	}

	@Override
	public void createEntity(Message t) {
		try {
			messageDao.create(t);
		} catch (NullPointerException e) {
			logger.error(e.getMessage(), e);
		} catch (HibernateException e) {
			logger.error(e.getMessage(), e);
		}

	}

	@Override
	public void deleteEntityById(Integer id) {
		try {
			Message message = messageDao.read(id);
			if (message != null) {
				messageDao.delete(message);
			}
		} catch (NullPointerException e) {
			logger.error(e.getMessage(), e);
		} catch (HibernateException e) {
			logger.error(e.getMessage(), e);
		}

	}

	@Override
	public void updateEntity(Message t) {
		try {

			Message message = messageDao.read(t.getMessageId());
			message.setReaded(t.isReaded());
			messageDao.update(message);
		} catch (NullPointerException e) {
			logger.error(e.getMessage(), e);
		} catch (HibernateException e) {
			logger.error(e.getMessage(), e);
		}

	}

	@Override
	public Message getEntityById(Integer id) {
		Message message = null;
		try {

			message = messageDao.read(id);
			if (message == null) {
				throw new NullPointerException(MESSAGE_NOT_FOUND);
			}
		} catch (NullPointerException e) {
			logger.error(e.getMessage(), e);
		} catch (HibernateException e) {
			logger.error(e.getMessage(), e);
		}
		return message;
	}

	@Override
	public List<Message> getEntityList() {
		List<Message> messages = null;
		try {
			messages = messageDao.readAll();
		} catch (NullPointerException e) {
			logger.error(e.getMessage(), e);
		} catch (HibernateException e) {
			logger.error(e.getMessage(), e);
		}
		return messages;

	}

	@Override
	public List<Message> getUserSendedMessageList(User user) {
		List<Message> res = null;
		try {
			user = userDao.read(user.getUserId());

			res = messageDao.findMessagesBySender(user);

		} catch (NullPointerException e) {
			logger.error(e.getMessage(), e);
		} catch (HibernateException e) {
			logger.error(e.getMessage(), e);
		}

		return res;
	}

	@Override
	public List<Message> getUserReceivedMessageList(User user) {
		List<Message> res = null;
		try {
			user = userDao.read(user.getUserId());

			res = messageDao.findMessagesByReceiver(user);

		} catch (NullPointerException e) {
			logger.error(e.getMessage(), e);
		} catch (HibernateException e) {
			logger.error(e.getMessage(), e);
		}

		return res;
	}

	@Override
	public List<Message> getUserMessageList(User user) {
		List<Message> res = null;
		try {

			user = userDao.read(user.getUserId());

			res = messageDao.findMessagesByReceiver(user);
			res.addAll(messageDao.findMessagesBySender(user));

		} catch (NullPointerException e) {
			logger.error(e.getMessage(), e);
		} catch (HibernateException e) {
			logger.error(e.getMessage(), e);
		}

		return res;
	}

}
